/**
 * 
 */
package cn.agiledata.bank.facade.helper;

import java.util.Map;

import cn.agiledata.bank.common.exception.BusinessException;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.myspace.exception.MySpaceException;
import cn.agiledata.sms.SmsService;

import com.vnv.infrastructure.email.service.IMailService;
import com.vnv.infrastructure.email.service.VNVMail;

/**
 * 用户通知助手，目前使用短信，邮件
 * 
 * @author 马贤波
 * 
 * 2006-7-18
 */
public class NotificationHelper {

	private SmsService smsService;

	private IMailService mailService;

	private TemplateHelper template;

	/**
	 * 通过短信验证手机号码
	 * 
	 * @param mobile
	 * @param context
	 * @throws BusinessException
	 */
	public void verifyMobileBySMS(String mobile, Map context)
			throws MySpaceException {

		String text = StringUtil.appendWebSiteUrl(this.template.mergeContent(
				"sms-verification.vm", context));
		try {
			this.smsService.sendMessage(mobile, text);
		} catch (Exception e) {
			e.printStackTrace();
            throw MySpaceException.SMS_SEND_ERROR;
		}
	}
	
	/**
	 * 通过短信发送申请重置密码的确认码
	 * 
	 * @param mobile
	 * @param context
	 * @throws BusinessException
	 */
	public void applyResetPasswordBySMS(String mobile, Map context)
		throws MySpaceException {
		String text = StringUtil.appendWebSiteUrl(this.template.mergeContent(
				"sms-applyResetPassword.vm", context));
		try {
			this.smsService.sendMessage(mobile, text);
		} catch (Exception e) {
			e.printStackTrace();
            throw MySpaceException.SMS_SEND_ERROR;
		}
	}

	/**
	 * 通过短信发登陆密码
	 * 
	 * @param mobile
	 * @param context
	 * @throws BusinessException
	 */
	public void returnLoginPasswordBySMS(String mobile, Map context)
			throws BusinessException {
		String text = this.template.mergeContent(".vm",
				context);
		try {
			this.smsService.sendMessage(mobile, text);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 发送公共事业费缴费通知
	 * 
	 * @param mobile
	 * @param context
	 * @throws BusinessException
	 */
	public void sendPublicFeeSms(String mobile, Map context)
			throws BusinessException {
		String text = StringUtil.appendWebSiteUrl(this.template.mergeContent(
				"sms-publicFee.vm", context));
		try {
			this.smsService.sendMessage(mobile, text);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public IMailService getMailService() {
		return mailService;
	}

	public void setMailService(IMailService mailService) {
		this.mailService = mailService;
	}
	
	/**
	 * 发送邮件
	 * 
	 * @param email		邮件地址
	 * @param subject	标题
	 * @param template	模板名称
	 * @param param		模板参数
	 */
	public void sendEmail(String email,String subject,String template,Map param) {
		VNVMail mail = new VNVMail();
		mail.setEmailTo(email);
		mail.setSubject(subject);

		mail.setContext(param);
		mail.setTemplateName(template);
		mailService.sendMail(mail);
	}



	public SmsService getSmsService() {
		return smsService;
	}

	public void setSmsService(SmsService smsService) {
		this.smsService = smsService;
	}

	public TemplateHelper getTemplate() {
		return template;
	}

	public void setTemplate(TemplateHelper template) {
		this.template = template;
	}
}
